<?php 

class indexModel extends appModel
{
	
    /**
     * 获取所有优惠券信息
     * @return Ambigous <number, boolean, multitype:multitype: >
     */
    public function getAllCard()
    {
        $sql = " SELECT * FROM ".$this->table('activity_card')." ORDER BY ordering DESC ";
        $info = $this->getAll($sql);
        $count = count($info);
        if($info)
        {
            foreach($info as $k => $v)
            {
                $info[$k]['last'] = $k == ($count - 1) ? 1 : 0;
                $info[$k]['canUser'] = $v['card_num'] - $v['card_user'];
            }
        }
        return $info;
    }
    
    /**
     * 获取优惠券数量充当参与人数
     * @return Ambigous <string, boolean>
     */
    public function getAllCardNum()
    {
        $sql = " SELECT SUM(card_num) FROM ".$this->table('activity_card');
        return $this->getOne($sql);
    }
    
    /**
     * 获取当前参与的人数
     * @return Ambigous <string, boolean>
     */
    public function getNowCardNum()
    {
        $sql = " SELECT SUM(card_user) FROM ".$this->table('activity_card');
        return $this->getOne($sql);    
    }
    
    /**
     * 插入记录
     * @param unknown $info
     * @return Ambigous <boolean, resource>
     */
    public function insertRecode($info)
    {
        $data = yf_set_insert($info);
        $sql = " INSERT INTO ".$this->table('activity_recode')." ( ".$data['key']." ) VALUES( ".$data['val']." ) ";
        return $this->query($sql);
    }
    
    /**
     * 更新优惠券数量
     * @param unknown $id
     * @return Ambigous <boolean, resource>
     */
    public function updateCardNum($id)
    {
        $sql = " UPDATE ".$this->table('activity_card')." SET card_user=card_user+1 WHERE id='".$id."' ";
        return $this->query($sql);
    }
    
    /**
     * 判断是否已经授权
     * @param unknown $openid
     * @return Ambigous <string, boolean>
     */
    public function isExistOpenid($openid)
    {
        $sql = " SELECT COUNT(*) FROM ".$this->table('wxuser')." WHERE openid='".$openid."' ";
        return $this->getOne($sql);
    }
    
    /**
     * 获取用户信息
     * @param unknown $openid
     * @return Ambigous <boolean, multitype:>
     */
    public function getUserInfo($openid)
    {
        $sql = " SELECT id, openid, nickname, city_code, province_code FROM ".$this->table('wxuser')." WHERE openid='".$openid."' ";
        return $this->getRow($sql);
    }
    
    /**
     * 插入用户信息
     * @param unknown $info
     * @return Ambigous <boolean, resource>
     */
    public function insertWxuser($info)
    {
        $data = yf_set_insert($info);
        $sql = " INSERT INTO ".$this->table('wxuser')." ( ".$data['key']." ) VALUES( ".$data['val']." ) ";
        $this->query($sql);
        return $this->insert_id();
    }
    
    /**
     * 获取省份ID
     * @param unknown $province
     * @return number|Ambigous <string, boolean>
     */
    public function getProvince($province)
    {
        if(!$province)
        {
            return 0;
        }
        $sql = " SELECT id FROM ".$this->table('areas')." WHERE name LIKE '%".$province."%' AND pid='0' ";
        $id = $this->getOne($sql);
        if($id)
        {
            return $id;
        }
        else 
        {
            return 0;
        }
    }
    
    /**
     * 获取城市ID
     * @param unknown $city
     * @return number|Ambigous <string, boolean>
     */
    public function getCity($city)
    {
        if(!$city)
        {
            return 0;
        }
        $sql = " SELECT id FROM ".$this->table('areas')." WHERE name LIKE '%".$city."%' AND pid<>'0' ";
        $id = $this->getOne($sql);
        if($id)
        {
            return $id;
        }        
        else
        {
            return 0;
        }
    }
    
    /**
     * 获取用户记录
     * @param unknown $wxid
     * @param unknown $openid
     * @return Ambigous <boolean, multitype:>
     */
    public function getRecode($wxid, $openid)
    {
        $sql = " SELECT * FROM ".$this->table('activity_recode')." WHERE wxid='".$wxid."' AND openid='".$openid."' ";
        return $this->getRow($sql);
    }
    
    /**
     * 获取分享信息
     * @param unknown $code
     * @return Ambigous <boolean, multitype:>
     */
    public function getShareInfo($code)
    {
        $sql = " SELECT * FROM ".$this->table('wxshare')." WHERE share_code='".$code."' ";
        return $this->getRow($sql);
    }
    
    /**
     * 更新状态
     * @param unknown $wxid
     * @param unknown $openid
     * @return Ambigous <boolean, resource>
     */
    public function updateStatus($wxid, $openid)
    {
        $sql = " UPDATE ".$this->table('activity_recode')." SET status='1' WHERE wxid='".$wxid."' AND openid='".$openid."' ";
        return $this->query($sql);
    }
    
}